package com.linked.basemanage.permissioncontrol.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.linked.basemanage.permissioncontrol.bean.param.AddMenusToPowerParam;
import com.linked.basemanage.permissioncontrol.bean.po.PowerInfoPO;
import com.linked.basemanage.permissioncontrol.service.IPowerInfoService;
import com.linked.basemanage.permissioncontrol.service.IPowerMenusService;
import com.linked.universal.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author :dbq
 * @date : 2023/2/22 14:28
 * @description : desc
 */
@Slf4j
@RestController
@RequestMapping("powerinfo")
public class PowerInfoController {

    private final ObjectMapper mapper;

    private final IPowerInfoService powerInfoService;

    private final IPowerMenusService powerMenusService;

    @Autowired
    public PowerInfoController(ObjectMapper mapper, IPowerInfoService powerInfoService, IPowerMenusService powerMenusService) {
        this.mapper = mapper;
        this.powerInfoService = powerInfoService;
        this.powerMenusService = powerMenusService;
    }

    @PostMapping("addPower")
    Result addPower(@RequestBody PowerInfoPO param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("新增权限接口 入参：{}", mapper.writeValueAsString(param));
        }
        boolean ret = false;
        try {
            ret = powerInfoService.addPower(param);
        } catch (Exception e) {
            log.error("新增权限接口 异常！", e);
            return Result.error("系统异常，请联系管理员！");
        }
        return ret ? Result.success() : Result.ok(false, "保存失败！");
    }

    @PostMapping("addMenusToPower")
    Result addMenusToPower(@RequestBody AddMenusToPowerParam param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("给权限加菜单接口 入参：{}", mapper.writeValueAsString(param));
        }
        boolean ret = false;
        try {
            ret = powerMenusService.addMenusToPower(param);
        } catch (Exception e) {
            log.error("给权限加菜单接口 异常！", e);
            return Result.error("系统异常，请联系管理员！");
        }
        return ret ? Result.success() : Result.ok(false, "保存失败！");
    }

}
